package com.ruoyi.goods.service.impl;

import com.ruoyi.goods.domain.BmGoods;
import com.ruoyi.goods.domain.BmSpecification;
import com.ruoyi.goods.mapper.BatchListMapper;
import com.ruoyi.goods.mapper.BmGoodsMapper;
import com.ruoyi.goods.service.BatchListService;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author {那蓝桉}
 * @Date: 2023/08/07/ 9:16
 * @description
 */
@Service
public class BatchListServiceImpl implements BatchListService {
    @Autowired
    private BatchListMapper batchListMapper;
    @Autowired
    private BmGoodsMapper goodsMapper;
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Override
    public List<BmSpecification> getBatchList(BmGoods bmGoods) {
        List<BmSpecification> list = batchListMapper.getBatchList(bmGoods);
        for (BmSpecification goods : list) {
            String specificationName = goods.getSpecificationName();
            List<BmGoods> bmGoodsList = goods.getBmGoodsList();
            if (bmGoodsList != null) {
                for (BmGoods bmSpecification : bmGoodsList) {
                    String goodsName = bmSpecification.getGoodsName();
                    specificationName = goodsName+" "+specificationName;
                }
                // 更新商品名称
                goods.setSpecificationName(specificationName);
            }
        }
        return list;
//        List<BmGoods> batchList = batchListMapper.getBatchList(bmGoods);
//        ArrayList<BmGoods> list = new ArrayList<>();
//        for (BmGoods goods : batchList) {
//            if (goods.getBmSpecificationList().size()>0) {
//                ArrayList<BmSpecification> bmSpecifications = new ArrayList<>();
//                for (BmSpecification bmSpecification : goods.getBmSpecificationList()) {
//                    BmGoods bmGoods1 = goods;
//                    bmSpecifications.add(bmSpecification);
//                    bmGoods1.setBmSpecificationList(bmSpecifications);
//                    list.add(bmGoods1);
//                }
//
//            }else {
//                list.add(goods);
//            }
//        }
//        return list;
//        return batchListMapper.getBatchList(bmGoods);
    }

    @Override
    public int updateIsShelves1(Long[] ids) throws IOException {
        int i = batchListMapper.updateIsShelves1(ids);
        IndexRequest indexRequest=null;
        for (Long id : ids) {
            Map<String,Object> map=new HashMap<>();
            map.put("specificationId",id);
            List<Map<String, Object>> goodsList = goodsMapper.findGoodsByCategory(map);
            for (Map<String, Object> good : goodsList) {
                //添加所有商品
                indexRequest=new IndexRequest("goods");
                indexRequest.source("{\"mappings\":{\"_source\":{\"enabled\":true}}}", XContentType.JSON);
                indexRequest.id(good.get("specificationId")+"").source(good, XContentType.JSON);
                IndexResponse indexResponse= restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
                //添加推荐商品
                if (Integer.parseInt(good.get("isRecommond").toString())==1){
                    indexRequest=new IndexRequest("recommondgoods");
                    indexRequest.source("{\"mappings\":{\"_source\":{\"enabled\":true}}}", XContentType.JSON);
                    indexRequest.id(good.get("specificationId")+"").source(good, XContentType.JSON);
                    IndexResponse response= restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
                }
            }
        }
        return i;
    }

    @Override
    public int updateIsShelves0(Long[] ids) throws IOException {
        int i = batchListMapper.updateIsShelves0(ids);
        DeleteRequest deleteRequest=null;
        for (Long id : ids) {
            deleteRequest =new DeleteRequest("goods",id.toString());
            restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
            deleteRequest =new DeleteRequest("recommondgoods",id.toString());
            restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        }
        return i;
    }

    @Override
    public int selectGoodsId(Long goodsId) {
        return batchListMapper.selectGoodsId(goodsId);
    }

}
